﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Web;

namespace ONTS.WEB
{
    public class BitmapToArray
    {
        const int MAX_WIDTH = 600;
        Bitmap image;
        public BitmapToArray(Bitmap image)
        {
            this.image = image;
            MakeExcelSafe();
        }

        private void Resize(int width, int height)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(image, 0, 0, width, height);
            g.Dispose();
        }

        private void MakeExcelSafe()
        { 
            if (image.Width > MAX_WIDTH)
            {
                int f, h;
                f = (int)image.Width / MAX_WIDTH;
                h = (int)image.Height / f; 
                Resize(MAX_WIDTH, h);
            }
        }

        public int[,] GetArray()
        {
            int[,] arr = new int[image.Width, image.Height];
            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    arr[x, y] = image.GetPixel(x, y).ToArgb();
                }
            }
            return arr;
        }
    }
}